import { Object3D } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';
import { DomObject } from '../utils/object.js';


/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @constructor
 */
class RemoveObjectCommand extends Command {
	private object:Object3D;
	private parent:Object3D|null;
	private index:number|undefined;
	constructor( editor:Editor, object:Object3D ) {

		super( editor );

		this.type = 'RemoveObjectCommand';
		this.name = 'Remove Object';

		this.object = object;
		this.parent = ( object !== undefined ) ? object.parent : null;
		if ( this.parent !== null ) {

			this.index = this.parent.children.indexOf( this.object );

		}

	}

	execute() {
		if(this.object instanceof DomObject){
			this.object.domElement.remove();
		}
		this.editor.removeObject( this.object );
		this.editor.deselect();

	}

	undo() {
		if(this.parent)
		this.editor.addObject( this.object, this.parent, this.index );
		this.editor.select( this.object );

	}

}

export { RemoveObjectCommand };
